<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class UserInfoAuth extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Article_Model');
		$this->load->helper('url_helper');
		$this->load->model('Users_Model');
		$this->load->library('session');
	}
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 *        http://example.com/index.php/welcome
	 *    - or -
	 *        http://example.com/index.php/welcome/index
	 *    - or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */


	public function auth(){
//		$appid = "wx6d03a0a1108166e1";
		$appid = "wx5672506691fea394";
//		define("APPID", "wx5672506691fea394"); //appid值
		$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri=http://www.fddiaoyu.com/index.php/UserInfoAuth&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
//		$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri=http://wechat.fddiaoyu.com/index.php/UserInfoAuth&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
		header("Location:".$url);

//		$array=array('user'=>'tlix');
//		$this->session->set_userdata($array);
//		header("Location:".$this->session->userdata('uri'));

	}

	public function index(){
//		$appid = "wx6d03a0a1108166e1";
//		$secret = "1d25a94f42f8167185b47a14d3bf4c18";

		$appid = "wx5672506691fea394";
		$secret = "a10b06bf4f7519152bad03311f212e65";

		$code =$this->input->get('code');
		$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$get_token_url);
		curl_setopt($ch,CURLOPT_HEADER,0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
		$res = curl_exec($ch);
		curl_close($ch);
		$json_obj = json_decode($res,true);

//根据openid和access_token查询用户信息
		if(!array_key_exists('access_token', $json_obj)){
			return;
		}
		$access_token = $json_obj['access_token'];
		$openid = $json_obj['openid'];
		//print_r($access_token);

		$access_token = $json_obj['access_token'];
		$openid = $json_obj['openid'];
		$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';

		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$get_user_info_url);
		curl_setopt($ch,CURLOPT_HEADER,0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
		$res = curl_exec($ch);
		curl_close($ch);

//解析json
		$user_obj = json_decode($res,true);
		$this->save_userinfo_todb($user_obj);
		$dataArray=array('user'=>$user_obj);
		$this->session->set_userdata($dataArray);
		//print_r($user_obj);
		header("Location:".$this->session->userdata('uri'));

	}
	private function save_userinfo_todb($user_obj){
		$user['nickname']=$user_obj['nickname'];
		$user['openid']=$user_obj['openid'];
		$user['unionid']=null;
		$user['province']=$user_obj['province'];
		$user['city']=$user_obj['city'];
		$user['headimgurl']=$user_obj['headimgurl'];
		$user['is_deleted']='N';
		$user['username']=null;
		$user['password']=null;
		$user['email']=null;
		$user['mobile']=null;
		$user['role']=null;
		$user['office']=null;
		$user['is_verifyed']=0;
		$user['court']=null;
		$user['real_name']=null;
		$user['gender']=null;
		$user['office_name']=null;
		$users=$this->Users_Model->get_user_by_openid($user_obj['openid']);
		print_r($users);
		if(empty($users)||count($users)==0){
			$this->insert_userInfo_to_db($user);
		}else{
			$old_user=$users[0];
			$id=$old_user['id'];
			$user['id']=$id;
			$this->update_userInfo_to_db($user);
		}
	}

	private function insert_userInfo_to_db($user){
		$user['gmt_create']=date("Y-m-d H:i:s" ,time());
		$this->Users_Model->add_user($user);
	}

	private function update_userInfo_to_db($user){
		$user['gmt_modify']=date("Y-m-d H:i:s" ,time());
		$this->Users_Model->update_user_by_id($user);
	}

}


